var register = function () {

    return {

        //main function to initiate the module
        init: function () {
            jQuery('#sample_1 .group-checkable').change(function () {
                var set = jQuery(this).attr("data-set");
                var checked = jQuery(this).is(":checked");
                jQuery(set).each(function () {
                    if (checked) {
                        $(this).attr("checked", true);
                    } else {
                        $(this).attr("checked", false);
                    }
                });
                jQuery.uniform.update(set);
            });
            jQuery('#sample_1 .checkboxes').each(function () {
                jQuery(this).change(function () {
                    var checked = jQuery(this).is(":checked");
                    var bool = true;
                    if (checked) {
                        jQuery('#sample_1 .checkboxes').each(function () {
                            if (!jQuery(this).is(":checked")) {
                                bool = false;
                            }
                        });
                        if (bool) {
                            jQuery('#sample_1 .group-checkable').attr("checked", true);
                        }
                    } else {
                        jQuery('#sample_1 .group-checkable').attr("checked", false);
                    }
                    jQuery.uniform.update('#sample_1 .group-checkable');
                });
            });

        },
        sendEmail: function () {
            $("#sendEmailBt").on("click", function () {
                $("#organization").val($("#company_name").val());

                App.blockUI(window, true);
                var form = $("#sendForm");

                $.ajax({
                    type: "POST",
                    cache: false,
                    url: $(form).attr("action"),
                    dataType: "json",
                    data: $(form).serialize(),
                    success: function (res) {

                        App.unblockUI(window);
                        if (res.code == 0) {
                            toastr["success"](res.message, "信息提示");
                            $("input[type=checkbox]").parent().each(function () {
                                var $this = $(this);
                                if ($this.hasClass("checked")) {
                                    $("input[type=checkbox]", $this).trigger("click");
                                }
                            });
                        } else {
                            if (res.ids == undefined || res.ids.length == 0) {
                                toastr["error"](res.message, "信息提示");
                                $("input[type=checkbox]").parent().each(function () {
                                    var $this = $(this);
                                    if ($this.hasClass("checked")) {
                                        $("input[type=checkbox]", $this).trigger("click");
                                    }
                                });
                            } else {
                                if (res.ids.length >= 1) {
                                    App.callBackConfirm(res.message, "#", function () {
                                        register.sendEmailA(res);
                                        return true;
                                    });
                                } else {
                                    register.sendEmailA(res);
                                }
                            }
                        }

                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        App.unblockUI(window);
                        document.write(xhr.responseText);
                    },
                    async: false
                });

                return false;
            });
        },
        sendEmailA: function (res) {

            App.blockUI(window, true);
            var form = $("#sendFormA");

            $.ajax({
                type: "POST",
                cache: false,
                url: $(form).attr("action"),
                dataType: "json",
                data: {
                    "organization": $("#company_name").val(),
                    "ids": res.ids.join(",")
                },
                success: function (res) {

                    App.unblockUI(window);
                    if (res.code == 0) {
                        toastr["success"](res.message, "信息提示");
                        $("input[type=checkbox]").parent().each(function () {
                            var $this = $(this);
                            if ($this.hasClass("checked")) {
                                $("input[type=checkbox]", $this).trigger("click");
                            }
                        });

                    } else {
                        if (res.ids == undefined || res.ids.length == 0) {
                            toastr["error"](res.message, "信息提示");
                            $("input[type=checkbox]").parent().each(function () {
                                var $this = $(this);
                                if ($this.hasClass("checked")) {
                                    $("input[type=checkbox]", $this).trigger("click");
                                }
                            });
                        } else {
                            if (res.ids.length >= 1) {
                                App.callBackConfirm(res.message, "#", function () {
                                    register.sendEmailA(res);
                                    return true;
                                });
                            } else {
                                register.sendEmailA(res);
                            }
                        }
                    }

                },
                error: function (xhr, ajaxOptions, thrownError) {
                    App.unblockUI(window);
                    document.write(xhr.responseText);
                },
                async: false
            });

        },
        selectDataByKey: function () {
            $('.show-key').on('keypress', function (e) {
                if (e.which == 13) {
                    $('#pageNo').val(1);
                    $("#searchBt").click();
                    return false;
                }
            });
        },
    };

}();